package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.ParseState;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;

/**
 *
 * 草根博客 解析类
 *
 * @author Chase  E-mail:594126573@qq.com
 *
 * @version 创建时间：2012-12-3  上午10:55:37
 */
public class CaogenBlogAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://.*.caogen.com/blog/.*/[0-9]*.html"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		String html = urlMeta.getHtml();
		if (html== null) {
		}
		NewsMeta blog = new NewsMeta();
		blog.setUrl(url);
		if(html.contains("alert(\"网络服务错误\")")){
			blog.setUpdateUrl(ParseState.ERR404.toString());
			return blog;
		}
		String title = null;
		String content = null;
		String author=null;
		Long date = null;
		Integer commentNum=0;

		Document doc = Jsoup.parse(html);
		title  = doc.select("span#Blog_Infor").text();
		date = DateUtils.matchDate(doc.select("span#Intime").text());
		content = HtmlCleaner.getContentHtml(url,  doc.select("span#Content"));
		String textComment = doc.select("span#infor_comment1_Repeater1__ctl0_Floor").text();

		if (textComment.indexOf("[") != -1) {
			textComment = textComment.substring(textComment.indexOf("[")+1);
			if (textComment.indexOf("楼]") != -1) {
				textComment = textComment.substring(0, textComment.indexOf("楼]"));
				try {
					commentNum = Integer.parseInt(textComment.trim());
				} catch (NumberFormatException e) {
				}
			}
		}

		author = doc.select("span#Head1_Person").text();
		blog.setAuthor(author);
		blog.setTitle(title);
		blog.setContent(content);
		blog.setDate(date);
		blog.setCommentNum(commentNum);
		String updateUrl = null;
		String updateUrlId = null;

		if (url.indexOf("/") != -1) {
			String[] split = url.split("/");
			if (split.length>0) {
				updateUrlId = split[split.length-1];
				if (updateUrlId.indexOf(".") != -1) {
					updateUrlId = updateUrlId.substring(0, updateUrlId.indexOf("."));
					updateUrl = "http://api.share.baidu.com/getnum?url=http%3A%2F%2Fwww.caogen.com%2Fblog%2Finfor_detail%2F"+updateUrlId+".html&callback=bdShare.fn._getShare&type=load&t="+System.currentTimeMillis()+"";
				}
 			}
		}

		if (updateUrl != null) {
			blog.setUpdateUrl(updateUrl);
			NewsMeta update = Update(blog);
			return update;
		}
		return blog;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			/*
			 * update deliver
			 */
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL=null;
				try {
					responseToURL = CrawlHTML.responseToURL(updateUrl);
				} catch (Exception e1) {
				}
				if(responseToURL!=null){
					String deliverNumStr = null;
					Integer deliverNum = 0;
					String html=responseToURL.getHtml();

					if (!StringUtils.isNullOrEmpty(html)) {
						if (html.indexOf("[") != -1) {
							int indexOf = html.indexOf("[");
							deliverNumStr = html.substring(indexOf+1);
							if (deliverNumStr.indexOf(",") != -1) {
								deliverNumStr = deliverNumStr.substring(0, deliverNumStr.indexOf(","));
							}
							try {
								deliverNum = Integer.parseInt(deliverNumStr);
							} catch (Exception e) {
							}
						}
						//meta.setDeliverNum(deliverNum);
					}
				}
			}

			/*
			 * update comment
			 */
			String updateUrl2 = meta.getUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl2);
				if(responseToURL!=null){
					Integer commentNum = 0;
					String html=responseToURL.getHtml();

					Document doc = Jsoup.parse(html);

					if (!StringUtils.isNullOrEmpty(html)) {
						String textComment = doc.select("span#infor_comment1_Repeater1__ctl0_Floor").text();

						if (textComment.indexOf("[") != -1) {
							textComment = textComment.substring(textComment.indexOf("[")+1);
							if (textComment.indexOf("楼]") != -1) {
								textComment = textComment.substring(0, textComment.indexOf("楼]"));
								try {
									commentNum = Integer.parseInt(textComment.trim());
								} catch (NumberFormatException e) {
								}
							}
						}
						meta.setCommentNum(commentNum);
						return meta;
					}
				}
			}
		}
		return null;
	}


	public static void main(String[] args) {
		String url = "http://www.caogen.com/blog/Infor_detail/56404.html";
		CaogenBlogAnalyse test = new CaogenBlogAnalyse();
		if(test.isDetailPage(url)){
			UrlMeta responseToURL = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = test.parserHtml(responseToURL);
			System.out.println(parserHtml);
		}else{
			System.out.println("不符合规则");
		}
	}





	
}
